package com.yesun.task.pipeline.quartz;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Map;

/**
 * @Author yesun
 * @Descritpion
 * @Date 2023/3/24 20:19
 * @Version 1.0
 **/
@Slf4j
public class JobSchedule {

    static StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
    static String JOB_PREFIX = "SPIDER_JOB_";
    static String TRIGGER_PREFIX = "SPIDER_TRIGGER_";

    //添加JOB
    public static void addJob(String jobName, Map map, String cron, Class clazz) throws SchedulerException {
        //调度器
        Scheduler scheduler = schedulerFactory.getScheduler();

        //任务实例
        JobDetail jobDetail = JobBuilder.newJob(clazz)
                .withIdentity(getJobKey(jobName))
                .usingJobData(getJobDataMap(map)) //JOB相关参数
                .build();

        //触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(getTriggerKey(jobName))
                .withSchedule(CronScheduleBuilder.cronSchedule(cron)) //支持cron表达式
                .build();

        //让调度器关联任务和触发器
        scheduler.scheduleJob(jobDetail, trigger);
        log.info("成功加载JOB:" + jobName + " cron:" + cron);
        //判断调度器是否启动
        if(!scheduler.isShutdown()) {
            scheduler.start();
        }
    }

    public static void removeJob(String jobName) throws SchedulerException {
        //调度器
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.pauseJob(getJobKey(jobName));
        scheduler.unscheduleJob(getTriggerKey(jobName));
        scheduler.deleteJob(getJobKey(jobName));
        log.info("移除任务：{}", jobName);
    }

    public static void clear() throws SchedulerException {
        //调度器
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.clear();
        log.info("移除所有任务！");
    }

    public static void startJobs() throws SchedulerException {
        //调度器
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        log.info("启动所有任务");
    }

    public static void stopJobs() throws SchedulerException {
        //调度器
        Scheduler scheduler = schedulerFactory.getScheduler();
        if(!scheduler.isShutdown()) {
            scheduler.shutdown();
            log.info("关闭所有任务！");
        }
    }


    private static JobKey getJobKey(String jobName) {
        JobKey jobKey = new JobKey(JOB_PREFIX + jobName);
        return jobKey;
    }

    private static TriggerKey getTriggerKey(String jobName) {
        TriggerKey triggerKey = new TriggerKey(TRIGGER_PREFIX + jobName);
        return triggerKey;
    }
    private static JobDataMap getJobDataMap(Map map) {
        return new JobDataMap(map);
    }


    public static void main(String[] args) throws SchedulerException {
//        //任务实例
//        JobDetail jobDetail = JobBuilder.newJob(WebJob.class)
//                .withIdentity("job1", "group1")
//                .usingJobData("url", "http://www.dataue.com") //JOB相关参数
//                .build();
//
//
//        //时间表达式
//        String cron = "*/5 * * * * ?";
//        //触发器
//        Trigger trigger = TriggerBuilder.newTrigger()
//                .withIdentity("trigger1", "group1")
//                .withSchedule(CronScheduleBuilder.cronSchedule(cron)) //支持cron表达式
////                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5))
//                .build();
//
//        //每隔5秒执行一次：*/5 * * * * ?
//        //每隔1分钟执行一次：0 0/1 * * * ?
//        //每天23点执行一次：0 0 23 * * ?
//        //每天23点执行一次：0 0 10,14 * * ? 每天10点和14点执行
//        //每月最后一天23点执行一次：0 0 23 L * ?
//
//
//        //让调度器关联任务和触发器
//        scheduler.scheduleJob(jobDetail, trigger);
//        scheduler.start();


    }


}
